#!/bin/env bash
# =================================================
# Author: WangChenChen<932560435@qq.com>          #
# Description: MySQL automatic installation tool  #
# =================================================

# MySQL configuration
mysql_version="8.0.24"
# MySQL root 用户 的密码
mysql_password="mht123456"

# MySQL 新建 用户 test123
user_test_name="test123"

# MySQL 新建 用户 test123 的密码
user_test_password="mht123456"

# MySQL 安装目录
mysql_install_dir="/usr/local/mysql"
mysql_data_dir="${mysql_install_dir}/data"

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:${mysql_install_dir}/bin

InstallMySQL() {
  # CentOS 8 mysql: error while loading shared libraries: libncurses.so.5
  yum install ncurses ncurses-compat-libs -y
  # Install dependencies
  setenforce 0
  yum -y install epel-release
  yum -y install wget tar jemalloc jemalloc-devel gcc gcc-c++

	mkdir -p ${mysql_data_dir}

	# 腾讯云镜像地址。随意选一个下载的快的！
	download_mysql="https://mirrors.cloud.tencent.com/mysql/downloads"
	# 清华大学镜像源
	# download_mysql="https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads"
	# 网易镜像
	# download_mysql="http://mirrors.163.com/mysql/Downloads"

	# Download MySQL
	cd /mnt
	useradd -M -s /sbin/nologin mysql >/dev/null 2>&1
	wget -c ${download_mysql}/MySQL-8.0/mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.xz

	echo "======================= Start installing MySQL ======================="

	tar xJf mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.xz
	\mv mysql-${mysql_version}-linux-glibc2.12-x86_64/* ${mysql_install_dir}

	if [[ -d "${mysql_install_dir}/support-files" ]]; then
		rm -rf mysql-${mysql_version}-linux-glibc2.12-x86_64
	else
		rm -rf ${mysql_install_dir}
		echo "Error: MySQL install failed, Please contact the author"
		kill -9 $$
	fi

	# Initialize the database
	${mysql_install_dir}/bin/mysqld --initialize-insecure --user=mysql --basedir=${mysql_install_dir} --datadir=${mysql_data_dir}
	chown -R root . ${mysql_install_dir}
	chown -R mysql.mysql ${mysql_data_dir}
	\cp -f ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
	sed -i "s@^basedir=.*@basedir=${mysql_install_dir}@" /etc/init.d/mysqld
	sed -i "s@^datadir=.*@datadir=${mysql_data_dir}@" /etc/init.d/mysqld
	sed -i "s@/usr/local/mysql@${mysql_install_dir}@g" ${mysql_install_dir}/bin/mysqld_safe
	sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/lib64/libjemalloc.so@' ${mysql_install_dir}/bin/mysqld_safe
	echo 'PATH=$PATH:'${mysql_install_dir}'/bin' >/etc/profile.d/mariadb_renwole.com.sh

	# my.conf configuration
	cat > /etc/my.cnf << EOF
[client]
port = 3306
default-character-set = utf8mb4

[mysql]
prompt="MySQL [\\d]> "
no-auto-rehash

[mysqld]
port = 3306
default_authentication_plugin = mysql_native_password

basedir = ${mysql_install_dir}
datadir = ${mysql_data_dir}
pid-file = ${mysql_data_dir}/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1

init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
collation-server = utf8mb4_0900_ai_ci

EOF
	# Set up MySQL
	chmod 600 /etc/my.cnf
	chmod +x /etc/init.d/mysqld
	systemctl enable mysqld
	systemctl daemon-reload
  systemctl start mysqld
  ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "create user root@'127.0.0.1' identified by \"${mysql_password}\";"
  ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "grant all privileges on *.* to root@'127.0.0.1' with grant option;"
 	${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "grant all privileges on *.* to root@'localhost' with grant option;"
  ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -e "alter user root@'localhost' identified by \"${mysql_password}\";"
  ${mysql_install_dir}/bin/mysql -uroot -p${mysql_password} -e "reset master;"

	# 创建用户
	${mysql_install_dir}/bin/mysql -uroot -hlocalhost -p${mysql_password} -e "create user \"${user_test_name}\"@'%' identified by \"${user_test_password}\";"
 	${mysql_install_dir}/bin/mysql -uroot -hlocalhost -p${mysql_password} -e "grant all privileges on *.* to \"${user_test_name}\"@'%' with grant option;"
  ${mysql_install_dir}/bin/mysql -uroot -hlocalhost -p${mysql_password} -e "ALTER USER \"${user_test_name}\"@'%' IDENTIFIED WITH mysql_native_password BY \"${user_test_password}\";"
	${mysql_install_dir}/bin/mysql -uroot -hlocalhost -p${mysql_password} -e "flush privileges;"

	# 检查防火墙状态
	firewallStatus=`ps -ef|grep firewalld|grep -v grep|wc -l`
  if [ $firewallStatus = 1 ];then
		echo "防火墙 开启 3306 端口"
		firewall-cmd --permanent --zone=public --add-port=3306/tcp
		firewall-cmd --reload
  else
    echo "Firewall Service is stop..."
  fi

  rm -rf /etc/ld.so.conf.d/{mysql,mariadb,percona,alisql}*.conf
  echo "${mysql_install_dir}/lib" > /etc/ld.so.conf.d/mysql_renwole.com.conf
  ldconfig
	echo "======================= MySQL Server ======================="
	echo "installing location : ${mysql_install_dir}    database: ${mysql_data_dir}"
	echo "account: root    password: ${mysql_password}"
	echo "account: ${user_test_name}    password: ${user_test_password}"
}

UninstallMySQL(){
	echo "======================= Uninstall MySQL ======================="
	# 先暂停mysql服务
	systemctl stop mysqld
	systemctl disable mysqld
	#yum卸载mysql
	yum remove mysql -y
	#yum卸载相关依赖
	yum remove mysql57-community-release-el7-11.noarch -y
	yum remove mysql-community-libs-5.7.34-1.el7.x86_64 -y
	yum remove mysql-community-common-5.7.34-1.el7.x86_64 -y
	yum remove mariadb -y
	rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
	#删除mysql相关文件夹
	rm -rf /var/lib/mysql \
	/var/lib/mysql \
	/usr/share/mysql \
	${mysql_install_dir} \
	/etc/my.cnf \
	/etc/init.d/mysqld \
	/usr/lib/systemd/system/mysqld.service

	systemctl daemon-reload
}

# 卸载
function uninstall()
{
  UninstallMySQL
}

# 安装
function install()
{
  UninstallMySQL
  InstallMySQL
  source /etc/profile
}

# 第一个参数 install|uninstall|i|u 中的一个
if [ "$1" = "" ];
then
    echo -e "\033[0;31m 未输入操作名 \033[0m  \033[0;34m {install|uninstall|i|u} \033[0m"
    exit 1
fi

case $1 in
    install)
    install;;
    i)
    install;;
    uninstall)
    uninstall;;
    u)
    uninstall;;
    *)

    echo -e "\033[0;31m Usage: \033[0m  \033[0;34m sh  $0  {install|uninstall|i|u} \033[0m
\033[0;31m Example: \033[0m
      \033[0;33m sh  $0  start \033[0m"
esac

